package com.txl.lzxl.webjx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    //    public static final String URL_MAIN = "http://dy.ittun.com/index.html";
    public static final String URL_MAIN = "http://v.2345.com/vip/list/--movie-----.html";
    private RecyclerView[] mRecyclerViews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerViews = new RecyclerView[]{(RecyclerView) findViewById(R.id.rc_1), (RecyclerView) findViewById(R.id.rc_2),
                (RecyclerView) findViewById(R.id.rc_3), (RecyclerView) findViewById(R.id.rc_4),
                (RecyclerView) findViewById(R.id.rc_5)};
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Document doc = Jsoup.connect(URL_MAIN).get();
                    List<Map<String, String>> list;
                    Elements a1 = doc.getElementsByClass("selectList clearfix");
                    Elements li = a1.select("li");
                    Map<String, String> map;
                    final List<TypeName> typeNames = new ArrayList<TypeName>();
                    for (Element el : li) {
                        TypeName type = new TypeName();
                        type.setType_name(el.getElementsByClass("sTit").text());
                        for (Element e : el.getElementsByClass("pSelect")) {
                            Elements a = e.select("a");
                            list = new ArrayList<Map<String, String>>();
                            for (Element e2 : a) {
                                String href = e2.attr("href");
                                String text = e2.text();
                                map = new HashMap<String, String>();
                                map.put(text, href);
                                list.add(map);
                            }
                            type.setNames(list);
                        }
                        typeNames.add(type);


                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initView(typeNames);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    int count = 0;

    //
    private void initView(List<TypeName> names2) {


//        LinearLayout[] linearLayouts = {(LinearLayout) findViewById(ll1), (LinearLayout) findViewById(R.id.ll2)
//                , (LinearLayout) findViewById(R.id.ll3), (LinearLayout) findViewById(R.id.ll4), (LinearLayout) findViewById(R.id.ll5)};
//        TextView textView;
//        for (int i = 0; i < 5; i++) {
//
//            textView = new TextView(this);
//            textView.setText(names2.get(i).getType_name());
//            linearLayouts[i].addView(textView);
//            for (Map<String, String> map : names2.get(i).getNames()) {
//                for (Map.Entry<String, String> entry : map.entrySet()) {
//                    textView = new TextView(this);
//                    textView.setText(entry.getKey());
//                    linearLayouts[i].addView(textView);
//                }
//            }
//            count++;
//            if (count == linearLayouts.length)
//                count = 0;
//        }
        LinearLayoutManager linearLayoutManager = null;
        Adapter adapter;
        for (int i = 0; i < mRecyclerViews.length; i++) {
            linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerViews[i].addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
            mRecyclerViews[i].setLayoutManager(linearLayoutManager);
            adapter = new Adapter(getApplication(), names2.get(i));
            mRecyclerViews[i].setAdapter(adapter);
            adapter.setOnItemClickListener(new Adapter.MyItemClickListener() {
                @Override
                public void onItemClick(View view, String name) {
                    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
